package com.ice_hufs.medical_in_myhand;


import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.*;


public class login extends Activity{
	
	
	private EditText userIdET;
	private EditText userPwdET;
	String userId;
	String userPwd;
	String type;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
				, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_login);

		
		userIdET = (EditText) findViewById(R.id.usernameInput);
		userPwdET = (EditText) findViewById(R.id.passwordInput);
		
		Button user_radioBtn = (Button)findViewById(R.id.user_radioBtn);
		user_radioBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				type = "user";
			}
		});
		
		
		Button operator_radioBtn = (Button)findViewById(R.id.operator_radioBtn);
		operator_radioBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				type = "operator";
			}
		});
		
		Button loginBtn = (Button)findViewById(R.id.loginButton);
		loginBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				userId = userIdET.getText().toString();
				userPwd = userPwdET.getText().toString();
				login();

			}

		});
		
		Button joinBtn = (Button)findViewById(R.id.joinBtn);
		joinBtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Intent myIntent= new Intent(login.this, Choose_join.class);	
				startActivity(myIntent);
				finish();
			}
		});
		
	}
	private void login()
    {
        RequestParams params = new RequestParams();
        params.put("email", userId);
        params.put("password", userPwd);
        params.put("type", type);

        String url = "http://54.68.142.141:5000/login";

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(url, params, new AsyncHttpResponseHandler()
        {
			@Override
			public void onSuccess(int arg0, org.apache.http.Header[] arg1,
					byte[] arg2) {
				// TODO Auto-generated method stub
				try
                {
                    String content = new String(arg2, "UTF-8");
                    Log.d("login test", content);
                    
                    if (arg0 == 200)
                    {
                    	Toast toast = Toast.makeText(getApplicationContext(),"welcome", Toast.LENGTH_SHORT);
    					toast.setGravity(Gravity.CENTER, 0, 0);
    					toast.show();
    					if (type == "user"){
    						//Intent my_Intent= new Intent(login.this, MainActivity.class);
                            Intent my_Intent= new Intent(login.this, hospital_list.class);
    						startActivity(my_Intent);

                           // Intent myIntent2 = new Intent(getApplicationContext(), Selected_hospital.class);
                           // myIntent2.putExtra("email", userId);
                           // startActivity(myIntent2);

    					}else if(type == "operator"){
                            //Intent my_intent = new Intent(login.this, reservation_date.class);
                            //startActivity(my_intent);
                        }
    					
                    }
                }
                catch (UnsupportedEncodingException e)
                {
                    Toast.makeText(getBaseContext(), "Error occurred.", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
				
			}
			
			@Override
			public void onFailure(int arg0, org.apache.http.Header[] arg1,
					byte[] arg2, Throwable arg3) {
				// TODO Auto-generated method stub
				Log.d("login test", Integer.toString(arg0));
				if(arg0 == 400){
					Toast toast = Toast.makeText(getApplicationContext(),"fail to login", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}else if(arg0 == 404){
					Toast toast = Toast.makeText(getApplicationContext(),"Not found ID/PW", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
      	
			}
		
        });
    }

}

